<template>
  <div>
    <el-form label-position="right" :model="recruiterForm" :label-width="formLabelWidth" ref="recruiterFormRef" :rules="rules">
      <el-row>
        <el-col :span="11">
          <el-form-item label="企业名称" prop="enterpriseName">
            <el-input v-model="recruiterForm.enterpriseName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="recruiterForm.username" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="recruiterForm.name" placeholder="请输入姓名" maxlength="64"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="密码" prop="password">
            <el-input v-model="recruiterForm.password" placeholder="请输入密码" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="recruiterForm.contactPhone" placeholder="请输入联系电话" maxlength="64"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="recruiterForm.email" placeholder="请输入邮箱" maxlength="64"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="form-button-container">
      <el-button type="primary" @click="onSave">保存</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import {inject, onMounted, reactive, ref, toRefs} from 'vue';
import recruiterApi from '@/api/recruiterApi'
import type {FormInstance} from 'element-plus';
import {ElMessage} from 'element-plus'
import type {RecruiterForm} from "@/types/req/recruiterForm";
import type {Result} from '@/types/result'
import type {Recruiter} from "@/types/resp/recruiter";

const recruiterFormRef = ref<FormInstance | null>(null);
const recruiterForm = reactive<RecruiterForm>({
  enterpriseId: 0,
  enterpriseName: '',
  username: '',
  name: '',
  password: '',
  contactPhone: '',
  email: '',
})

const rules = reactive({
  enterpriseName: [
    { required: true, message: '请输入企业名称', trigger: 'blur' }
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' }
  ],
});
const dialogTop = ref<string>('')
const dialogWidth = ref<string>('')
const formLabelWidth = ref<string>('')

onMounted(() => {
  initRecruiterFormById(1)

  const globalState = inject('globalState', {} as { dialogWidth?: string, dialogTop?: string, formLabelWidth?: string })
  if (!globalState) {
    throw new Error('globalState is not provided');
  }
  const globalStateRefs = toRefs(globalState)
  if (globalStateRefs.formLabelWidth) {
    formLabelWidth.value = globalStateRefs.formLabelWidth.value || ''
  }
  if (globalStateRefs.dialogWidth) {
    dialogWidth.value = globalStateRefs.dialogWidth.value || ''
  }
  if (globalStateRefs.dialogTop) {
    dialogTop.value = globalStateRefs.dialogTop.value || ''
  }
});

const initRecruiterFormById = async (id: number) => {
  const resp: Result<Recruiter> = await recruiterApi.findById(id)
  if (resp && resp.code === 1 && resp.data) {
    Object.assign(recruiterForm, resp.data)
  }
}

const onSave = () => {
  if (!recruiterFormRef.value) {
    return
  }
  recruiterFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      const resp: Result<void> = await recruiterApi.save(recruiterForm)
      if (resp && resp.code === 1) {
        ElMessage.success('保存成功')
      } else {
        ElMessage.error('保存失败:' + (resp.msg ? resp.msg : ''))
      }
    }
  })
}
</script>

<style lang="scss">
</style>
